@file:Suppress("unused")

package com.sscl.baselibrary.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.sscl.baselibrary.R
import com.sscl.baselibrary.exts.getParameterizedType
import com.sscl.baselibrary.exts.saveAs
import com.sscl.baselibrary.exts.saveAsUnChecked
import java.lang.reflect.ParameterizedType

/**
 * 使用ViewBiding的fragment的基类
 *
 * @author alm
 */
abstract class BaseViewBindingWithStatusBarFragment<T : ViewBinding> : Fragment() {

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 属性声明
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    /* * * * * * * * * * * * * * * * * * * 常量属性 * * * * * * * * * * * * * * * * * * */

    @Suppress("PropertyName")
    protected val TAG: String = javaClass.simpleName

    /* * * * * * * * * * * * * * * * * * * 延时初始化属性 * * * * * * * * * * * * * * * * * * */

    @Suppress("MemberVisibilityCanBePrivate")
    protected lateinit var binding: T

    /**
     * 用于适配沉浸式状态栏的专用控件
     */
    private lateinit var statusView: View

    /* * * * * * * * * * * * * * * * * * * 私有可空属性 * * * * * * * * * * * * * * * * * * */

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 重写方法
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val contentView: View =
            inflater.inflate(R.layout.com_jackiepenghe_baselibrary_fragment_with_status_bar_base, container, false)
        statusView = contentView.findViewById(R.id.fragment_base_status_bar)
        doBeforeSetLayout()
        val frameLayout: FrameLayout = contentView.findViewById(R.id.base_frame_content)
        val type = getParameterizedType()
        val vbClass: Class<T> = type.saveAs<ParameterizedType>().actualTypeArguments[0].saveAs()
        val method = vbClass.getDeclaredMethod("inflate", LayoutInflater::class.java)
        binding = method.invoke(this, layoutInflater)!!.saveAsUnChecked()
        frameLayout.addView(binding.root)
        return contentView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initData()
        initViewData(savedInstanceState)
        setBinding()
        initEvents()
        doAfterAll()
    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 抽象方法
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    /**
     * 在设置布局之前进行的操作
     */
    protected abstract fun doBeforeSetLayout()

    /**
     * 初始化数据
     */
    protected abstract fun initData()

    /**
     * 初始化控件数据
     */
    protected abstract fun initViewData(savedInstanceState: Bundle?)

    /**
     * 设置绑定数据
     */
    protected abstract fun setBinding()

    /**
     * 初始化事件
     */
    protected abstract fun initEvents()

    /**
     * 在最后执行的操作
     */
    protected abstract fun doAfterAll()

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 自定义子类可用的方法
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    /**
     * 将整个fragment的所有布局隐藏
     */
    protected fun hideAll() {
        binding.root.visibility = View.GONE
    }

    /**
     * 将整个fragment的所有布局显示
     */
    protected fun showAll() {
        binding.root.visibility = View.VISIBLE
    }

    /**
     * 设置状态栏颜色
     *
     * @param color 状态栏颜色
     */
    protected fun setFragmentStatusColor(@ColorInt color: Int) {
        statusView.visibility = View.VISIBLE
        statusView.setBackgroundColor(color)
    }

    /**
     * 设置状态栏颜色
     *
     * @param colorRes 状态栏颜色
     */
    protected fun setFragmentStatusColorRes(@ColorRes colorRes: Int) {
        statusView.visibility = View.VISIBLE
        statusView.setBackgroundResource(colorRes)
    }

    /**
     * 隐藏Fragment的状态栏控件
     */
    protected fun hideFragmentStatusView() {
        statusView.visibility = View.GONE
    }

    /**
     * 显示Fragment的状态栏控件
     */
    protected fun showFragmentStatusView() {
        statusView.visibility = View.VISIBLE
    }
}